----------------------------------------------------------------------------------
-- Company:        EECS 452
-- Engineer:       Kurt Metzger
-- 
-- Create Date:    20:16:58 04/21/2008 
-- Design Name: 
-- Module Name:    pb_debounce2 - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity pb_debounce2 is
    Port ( pb_in : in  STD_LOGIC_VECTOR (3 downto 0);
           pb_out : out  STD_LOGIC_VECTOR (3 downto 0);
           clk : in  STD_LOGIC);
end pb_debounce2;

architecture Behavioral of pb_debounce2 is

   signal bounce_counter : std_logic_vector(19 downto 0);
   signal sr_pb0 : std_logic_vector (3 downto 0) := "0000";
   signal sr_pb1 : std_logic_vector (3 downto 0) := "0000";
   signal sr_pb2 : std_logic_vector (3 downto 0) := "0000";
   signal sr_pb3 : std_logic_vector (3 downto 0) := "0000";
   signal pbout : std_logic_vector (3 downto 0) := "0000";

begin

   pb_out <= pbout;

   monitor : process(pb_in, clk)
   begin
      if rising_edge(clk) then
         bounce_counter <= bounce_counter+1;
         if bounce_counter = 0 then
            sr_pb0 <= sr_pb0(2 downto 0) & pb_in(0);
            sr_pb1 <= sr_pb1(2 downto 0) & pb_in(1);
            sr_pb2 <= sr_pb2(2 downto 0) & pb_in(2);
            sr_pb3 <= sr_pb3(2 downto 0) & pb_in(3);
         end if;
     
         if sr_pb0 = "1111" then
            pbout(0) <= '1';
         elsif sr_pb0 = "0000" then
            pbout(0) <= '0';
         else
            pbout(0) <= pbout(0);
         end if;
         
         if sr_pb1 = "1111" then
            pbout(1) <= '1';
         elsif sr_pb1 = "0000" then
            pbout(1) <= '0';
         else
            pbout(1) <= pbout(1);
         end if;
         
         if sr_pb2 = "1111" then
            pbout(2) <= '1';
         elsif sr_pb2 = "0000" then
            pbout(2) <= '0';
         else
            pbout(2) <= pbout(2);
         end if;
         
         if sr_pb3 = "1111" then
            pbout(3) <= '1';
         elsif sr_pb3 = "0000" then
            pbout(3) <= '0';
         else
            pbout(3) <= pbout(3);
         end if;
         
      end if;
    
   end process;

end Behavioral;

